package com.lyw.GeoLocationClient.ui.view;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.easemob.chatui.domain.User;
import com.lyw.GeoLocationClient.App;
import com.lyw.GeoLocationClient.R;
import com.lyw.GeoLocationClient.utils.StringUtils;
import com.lyw.GeoLocationClient.utils.T;
import com.lyw.GeoLocationClient.service.LBSClient;
import timber.log.Timber;


public class AddRosterItemDialog extends AlertDialog implements
		DialogInterface.OnClickListener {

	private Activity mMainActivity;
//	private XXService mXxService;

	private Button okButton;
	private EditText userNameField;
	private EditText passwd;
	private EditText pid;
	private EditText name;
	private EditText unit;
	private EditText imei;

//	private GroupNameView mGroupNameView;
    private LBSClient mClient;

    private EditText[] sets;

	public AddRosterItemDialog(Activity mainActivity, int theme) {
		super(mainActivity, theme);
		mMainActivity = mainActivity;
        mClient = LBSClient.getInstance();
//		mXxService = service;

		setTitle(R.string.addFriend_Title);

		LayoutInflater inflater = (LayoutInflater) mainActivity
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View group = inflater.inflate(R.layout.addrosteritemdialog, null, false);
		setView(group);

		userNameField = (EditText)group.findViewById(R.id.add_contact_user_name);
		passwd = (EditText)group.findViewById(R.id.add_contact_passwd);
		pid = (EditText)group.findViewById(R.id.add_contact_pid);
		name = (EditText)group.findViewById(R.id.add_contact_name);
		unit = (EditText)group.findViewById(R.id.add_contact_unit);
		imei = (EditText)group.findViewById(R.id.add_contact_imei);
        if (App.DEBUG) {
            imei.setText(LBSClient.DEF_IMEI);
        }
        sets = new EditText[]{userNameField, passwd, pid, name, unit, imei};

//		mGroupNameView = (GroupNameView)group.findViewById(R.id.AddRosterItem_GroupName);
//		mGroupNameView.setGroupList(mClient.getGroupList());

		setButton(BUTTON_POSITIVE, mainActivity.getString(android.R.string.ok), this);
		setButton(BUTTON_NEGATIVE, mainActivity.getString(android.R.string.cancel),
				(OnClickListener)null);


	}

	public AddRosterItemDialog(Activity mainActivity, String jid) {
        this(mainActivity, -1);
		userNameField.setText(jid);
	}

    public AddRosterItemDialog(Activity mainActivity, int theme, String jid) {
        this(mainActivity, jid);
        userNameField.setText(jid);
    }

	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);

		okButton = getButton(BUTTON_POSITIVE);

	}

    private boolean isComplete() {
        for (EditText editText : sets) {
            String s = getEditTextValue(editText);
            if (StringUtils.isNullOrEmpty(s)) {
                return false;
            }
        }
        return true;
    }

    private void setEnable(boolean enable) {
        for (EditText editText : sets) {
            editText.setEnabled(enable);
        }
    }

    private void dumpInfo() {
        Timber.d("======= dump reg Info =====");
        for (EditText editText : sets) {
            Timber.d("=== " + getEditTextValue(editText));
        }
    }

	public void onClick(DialogInterface dialog, int which) {
        if (isComplete()) {
            setEnable(false);
            dumpInfo();
            User geotableItem = new User(getEditTextValue(userNameField), getEditTextValue(name),
                    getEditTextValue(pid), getEditTextValue(unit),
                    "", getEditTextValue(imei));
            mClient.addContact(getEditTextValue(userNameField), getEditTextValue(passwd), geotableItem);
//            mClient.listTable(getEditTextValue(userNameField), new Callback<GeotableList>() {
//                @Override
//                public void success(GeotableList geotableList, Response response) {
//                    if (geotableList != null) {
//                        if (geotableList.geotables != null && geotableList.geotables.size() > 0) {
//                            T.showShort(getContext(), R.string.has_same_user);
//                            return;
//                        }
//                    }
//                    User geotableItem = new User(getEditTextValue(userNameField), getEditTextValue(name),
//                            getEditTextValue(pid), getEditTextValue(unit),
//                            "", getEditTextValue(imei));
//
//                    mClient.addContact(getEditTextValue(userNameField), getEditTextValue(passwd), geotableItem);
//                }
//
//                @Override
//                public void failure(RetrofitError retrofitError) {
//                    T.showShort(getContext(), R.string.network_anomalies);
//                }
//            });

        } else {
            T.showShort(getContext(), R.string.field_not_null_notice);
            this.show();
        }
    }

    private String getEditTextValue(EditText text) {
        try {
            return text.getText().toString();
        } catch (NullPointerException e) {
            return "";
        }
    }

	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {

	}

	public void onTextChanged(CharSequence s, int start, int before, int count) {

	}

}
